/*************************************************************************
*																		 *
*				code by Miles Chen (v-mich) 2007.10.8					 *
*																		 *
*																		 *
*************************************************************************/

#pragma once

#include "commdef-int.h"

namespace System{namespace String{

	LPWSTR CopyTo(LPWSTR lpszDest, LPCWSTR lpszSrc)
	{
		//return Ntwcscpy(lpszDest, lpszSrc);

        //wchar_t * cp = lpszDest;

        //while( *cp++ = *lpszSrc++ )
        //        ;               /* Copy src over dst */

        //return( lpszDest );
		return wcscpy(lpszDest, lpszSrc);
	}

	LPWSTR Append(LPWSTR lpszDest, LPCWSTR lpszSrc)
	{
		//return Ntwcscat(lpszDest, lpszSrc);

        //wchar_t * cp = lpszDest;

        //while( *cp )
        //        cp++;                   /* find end of dst */

        //while( *cp++ = *lpszSrc++ ) ;   /* Copy src to end of dst */

        //return( lpszDest );             /* return dst */
		return wcscat(lpszDest, lpszSrc);
	}

	LPWSTR Join(LPWSTR lpszDest, LPCWSTR lpszSrc)
	{
		//return Ntwcscat(lpszDest, lpszSrc);

        //wchar_t * cp = lpszDest;

        //while( *cp )
        //        cp++;                   /* find end of dst */

        //while( *cp++ = *lpszSrc++ ) ;   /* Copy src to end of dst */

        //return( lpszDest );             /* return dst */
		return wcscat(lpszDest, lpszSrc);
	}

	LPBYTE Join(LPBYTE lpDest, LPCBYTE lpSrc)
	{
		return (LPBYTE)strcat((char*)lpDest, (const char*)lpSrc);
	}

	BOOL StartWith(LPCWSTR lpszString, LPCWSTR lpszSymbol)
	{
		while(*lpszSymbol)
		{
			if(*lpszSymbol++ != *lpszString++)
				return FALSE;
		}
		return TRUE;
	}

	BOOL Split(LPCWSTR lpszString, WCHAR nSymbol, LPWSTR lpszBuffer, int nDim1, int nDim2)
	{
		LPWSTR p = (LPWSTR)lpszString, q = lpszBuffer, t = lpszBuffer;

		while(*p)
		{
			if(*p != nSymbol)
			{
				*q++ = *p++;
			}
			else
			{
				*q = L'\0';
				p++;
				q = t + nDim1;
				t = q;
			}
		}
		*q = L'\0';

		return TRUE;
	}


	size_t Length(LPCWSTR lpszString)
	{
		//return Ntwcslen(lpszString);

        //const wchar_t *eos = lpszString;

        //while( *eos++ ) ;

        //return( (size_t)(eos - lpszString - 1) );
		return wcslen(lpszString);
	}

	size_t Length(LPCBYTE lpBuffer)
	{
		return strlen((const char*)lpBuffer);
	}

	int Compare(LPCWSTR lpszStr1, LPCWSTR lpszStr2)
	{
		//return Ntwcscmp(lpszStr1, lpszStr2);

        //int ret = 0 ;

        //while( ! (ret = (int)(*lpszStr1 - *lpszStr2)) && *lpszStr2)
        //        ++lpszStr1, ++lpszStr2;

        //if ( ret < 0 )
        //        ret = -1 ;
        //else if ( ret > 0 )
        //        ret = 1 ;

        //return( ret );
		return wcscmp(lpszStr1, lpszStr2);
	}

	int IndexOf(LPCWSTR lpszString, LPCWSTR lpszSymbol, int startIndex)
	{
		LPWSTR p = (LPWSTR)lpszString + startIndex, q = (LPWSTR)lpszSymbol;
		int index = 0;

		while(*p)
		{
			if(*q == *p)
			{
				q++;
				p++;
			}
			else
			{
				p++;
				q = (LPWSTR)lpszSymbol;
			}

			index++;

			if(!*q)
				return index - Length(lpszSymbol) + startIndex;
		}

		return -1;
	}

	BOOL Contains(LPCWSTR lpszString, LPCWSTR lpszSymbol)
	{
		return IndexOf(lpszString, lpszSymbol, 0) != -1;
	}

	int LastIndexOf(LPCWSTR lpszString, LPCWSTR lpszSymbol)
	{
		int nLastIndex = -1, index = -1;

		while((index = IndexOf(lpszString, lpszSymbol, index + 1)) != -1)
		{
			nLastIndex = index;
		}

		return nLastIndex;
	}

}}